import { Injectable } from '@nestjs/common';
import { CreateFriendDto } from './dto/create-friend.dto';
import { UpdateFriendDto } from './dto/update-friend.dto';
import { Friend } from './entities/friend.entity';
import { Repository } from 'typeorm';
import { InjectRepository } from '@nestjs/typeorm';
import { FindAllCategoryDto } from './dto/findAll-category.dto';

@Injectable()
export class FriendsService {
  constructor(
    @InjectRepository(Friend)
    private readonly friendsRepository: Repository<Friend>,
  ) {}

  async create(createFriendDto: CreateFriendDto) {
    const friend = this.friendsRepository.create({ ...createFriendDto });
    return this.friendsRepository.save(friend);
  }

  async findAll(query: FindAllCategoryDto) {
    return await this.friendsRepository.find({where:{...query}});
  }

  async findOne(id: number) {
    return await this.friendsRepository.findOne({where:{id}});
  }

  async update(id: number, updateFriendDto: UpdateFriendDto) {
    const friend = await this.friendsRepository.preload({id, ...updateFriendDto });
    return this.friendsRepository.save(friend);
  }

  async remove(id: number) {
    return await this.friendsRepository.delete(id)
  }
}
